import { BulletDirection, PlaneCamp } from "../Constant/Constant";
import PoolManager from "../Manager/PoolManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BulletControl extends cc.Component {

  @property
  speed: number = 400

  public direction: BulletDirection = BulletDirection.UP
  public from: PlaneCamp = PlaneCamp.PLAYER

  update(dt: number) {
    if (this.direction == BulletDirection.UP) {
      this.node.y += this.speed * dt
    } else if (this.direction == BulletDirection.DOWN) {
      this.node.y -= this.speed * dt
    }

    // 超出屏幕后销毁
    if (this.node.y > cc.view.getVisibleSize().height + this.node.height ||
      this.node.y < -this.node.height) {
      // 回收子弹
      PoolManager.instance.putNode(this.node)
    }

  }

  /**
   * 当碰撞产生的时候调用
   * @param  {Collider} other 产生碰撞的另一个碰撞组件
   * @param  {Collider} self  产生碰撞的自身的碰撞组件
   */
  onCollisionEnter(other: cc.Collider, self: cc.Collider): void {
    // 子弹触发碰撞后自毁
    // 回收子弹
    PoolManager.instance.putNode(this.node)
  }
}
